package com.pospi.pospihold.print.a920;

import android.content.Context;
import android.util.Log;

import com.pospi.pospihold.dto.CashierMsgDto;
import com.pospi.pospihold.dto.GoodsDto;
import com.pospi.pospihold.dto.OrderBeen;
import com.pospi.pospihold.dto.OrderDto;
import com.pospi.pospihold.util.Constant;
import com.pospi.pospihold.util.GetData;
import com.pospi.pospihold.util.PareseJson;
import com.pospi.pospihold.util.SaveOrderInfo;

import java.util.List;

/**
 * Created by Qiyan on 2016/7/19.
 */
public class PrintA920 {
    public static void prnVoucher(Context context, OrderBeen orderDto) {

        Log.i("PrintA920","打印机开始打印");
        String storeName = context.getSharedPreferences("StoreMessage", Context.MODE_PRIVATE).getString("Name", "");
        String value = context.getSharedPreferences("Token", Context.MODE_PRIVATE).getString("value", "");
        String[] names = value.split("\\,");
        String deviceNo = names[2];//得到token的值
        int which = context.getSharedPreferences("islogin", Context.MODE_PRIVATE).getInt("which", 0);
        List<CashierMsgDto> cashierMsgDtos = new PareseJson().parese(context.getSharedPreferences("cashierMsgDtos", Context.MODE_PRIVATE).getString("cashierMsgDtos", ""));

        List<GoodsDto> goodsDtos = SaveOrderInfo.changeToList(orderDto.getGoodsDetail());
        Log.i("PrintA920","打印机开始打印");
        PrnTest.prnInit();
        PrnTest.prnLeftIndent((short) 80);
        PrnTest.prnSetGray(3);
        PrnTest.prnDoubleHeight(1, 1);
        PrnTest.prnDoubleWidth(1, 1);
        PrnTest.prnStr(storeName + "\n", null);
        PrnTest.prnLeftIndent((short) 0);
        PrnTest.prnSetGray(0);
        PrnTest.prnLeftIndent((short) 0);
        PrnTest.prnFontSet((byte) 2, (byte) 2);
//        PrnTest.prnStr("设备号：" + deviceNo + "\n", null);
        PrnTest.prnStr("单据号：" + orderDto.getOrderNo() + "\n", null);
        PrnTest.prnStr("收银员：" + cashierMsgDtos.get(which).getName() + "\n", null);
        PrnTest.prnStr("单据日期：" + GetData.getDataTime() + "\n", null);
        PrnTest.prnStr("..............................................", null);
        PrnTest.prnFontSet((byte) 3, (byte) 3);
        PrnTest.prnStr("\n品名    单价     数量     金额    折扣\n", null);
        PrnTest.prnFontSet((byte) 2, (byte) 6);
        PrnTest.prnStr("..............................................\n", null);

        int numTotal = 0;
        double moneyTotal = 0;
        double money = 0.00;
        double disountTotal = 0.00;
        double discount = 0.00;
        for (int i = 0; i < goodsDtos.size(); i++) {
            GoodsDto goodsDto = goodsDtos.get(i);
            numTotal += goodsDto.getNum();
            discount = goodsDto.getDiscount();
            disountTotal += discount;
            money = goodsDto.getNum() * goodsDto.getPrice() - discount;
            moneyTotal += money;
            PrnTest.prnFontSet((byte) 2, (byte)2);
            PrnTest.prnStr(goodsDtos.get(i).getName() + "\n", null);
            PrnTest.prnStr("           " + goodsDto.getPrice() + "        " + goodsDto.getNum() + "      " + money + "       " + discount + "\n\n", null);
        }
        PrnTest.prnFontSet((byte) 2, (byte) 2);
        PrnTest.prnStr("..............................................\n", null);
        PrnTest.prnStr("\n合计                   " + numTotal + "      " + moneyTotal + "       " + disountTotal + "\n", null);
        PrnTest.prnStr("..............................................\n", null);


        PrnTest.prnFontSet((byte) 2, (byte) 2);
//        if (orderDto.getPayway().equals(Constant.PAYMENT_CASH_String)) {
            PrnTest.prnStr("付款合计：      " + orderDto.getAmount() + "\n", null);
            PrnTest.prnStr("实收            " + orderDto.getAmount() + "\n", null);
            PrnTest.prnStr("找零            " + 0 + "\n", null);
//        } else {
//            PrnTest.prnStr("付款合计： \n", null);
//            PrnTest.prnStr("        " + orderDto.getPayway() + "    " + orderDto.getYs_money() + "\n", null);
//        }
        PrnTest.prnFontSet((byte) 2, (byte) 2);
        PrnTest.prnStr("..............................................\n", null);
        PrnTest.prnStr("谢谢惠顾！\n", null);

        PrnTest.prnStart();
        isfinish();
        PrnTest.prnStep((short) 300);//重要：因为PrnTest.prnStep中加入了prnInit()方法，所以需要先打印前面的内容，否则会直接打印后面的内容！
        PrnTest.prnStart();
        //	PrnTest.finish();       !请确保不再使用PrintManager中的类再执行该方法，否则可能会出现异常

    }

    //判断是否打印完成，完成之后再执行prnInit()方法;
    private static void isfinish() {
        while (true) {
            if (PrnTest.prnStatus() == 0) {
                break;
            }
        }
    }
}
